import React, {Component} from 'react';
import {StyleSheet, Text, View, TouchableHighlight} from 'react-native';
import {Modal} from '@ant-design/react-native';
import * as WeChat from 'react-native-wechat-lib';
import {getAppParameters} from '~/api/wechat';
import {ownerPaymentBefore, ownerPayment} from '~/api/parking';

class CustomButton extends Component {
    render() {
        return (
            <TouchableHighlight style={styles.button} underlayColor="#a5a5a5" onPress={this.props.onPress}>
                <Text style={styles.buttonText}>{this.props.text}</Text>
            </TouchableHighlight>
        );
    }
}
class RNWeChatDemo extends Component {
    constructor(props) {
        super(props);
        //应用注册
        WeChat.registerApp('wxd487e39768c6f9ea', 'https://api.dev.lipopo.com.cn/').then(() => {});
    }
    GetAppParameters() {
        getAppParameters({token: '', code: ''}).then((r) => {
            console.log(r);
            if (r.code === 0) {
                console.log(r.data);
            } else {
                Modal.alert('提示', r.msg);
            }
        });
    }
    render() {
        return (
            <View style={{margin: 20}}>
                <Text style={styles.welcome}>微信好友/朋友圈分享实例</Text>
                <CustomButton
                    text="检查微信是否安装"
                    onPress={() => {
                        WeChat.isWXAppInstalled().then((isInstalled) => {
                            if (isInstalled) {
                                Modal.alert('提示', '已安装');
                            } else {
                                Modal.alert('提示', '没有安装微信软件， 请您安装微信之后再试');
                            }
                        });
                    }}
                />
                <CustomButton
                    text="检查微信版本号"
                    onPress={() => {
                        WeChat.getApiVersion().then((ver) => {
                            Modal.alert('提示', ver);
                        });
                    }}
                />

                <CustomButton
                    text="微信到好友-文本"
                    onPress={() => {
                        WeChat.isWXAppInstalled().then((isInstalled) => {
                            if (isInstalled) {
                                WeChat.shareText({text: '测试微信好友分享文本', scene: 0}).catch((error) => {
                                    Modal.alert('提示', error.errStr);
                                });
                            } else {
                                Modal.alert('提示', '没有安装微信软件， 请您安装微信之后再试');
                            }
                        });
                    }}
                />
                <CustomButton
                    text="微信好友分享-链接"
                    onPress={() => {
                        WeChat.isWXAppInstalled().then((isInstalled) => {
                            if (isInstalled) {
                                WeChat.shareWebpage({
                                    title: '微信好友测试链接',
                                    description: '分享自:江清清的技术专栏(www.lcode.org)',
                                    thumbImageUrl: 'http://mta.zttit.com:8080/images/ZTT_1404756641470_image.jpg',
                                    webpageUrl: 'http://www.lcode.org',
                                    scene: 0,
                                }).catch((error) => {
                                    Modal.alert('提示', error.errStr);
                                });
                            } else {
                                Modal.alert('提示', '没有安装微信软件，请您安装微信之后再试');
                            }
                        });
                    }}
                />
                <CustomButton
                    text="微信朋友圈分享-文本"
                    onPress={() => {
                        WeChat.isWXAppInstalled().then((isInstalled) => {
                            if (isInstalled) {
                                WeChat.shareText({text: '测试微信好友分享文本', scene: 1}).catch((error) => {
                                    Modal.alert('提示', error.errStr);
                                });
                            } else {
                                Modal.alert('提示', '没有安装微信软件，请您安装微信之后再试');
                            }
                        });
                    }}
                />
                <CustomButton
                    text="微信朋友圈分享-链接"
                    onPress={() => {
                        WeChat.isWXAppInstalled().then((isInstalled) => {
                            if (isInstalled) {
                                WeChat.shareWebpage({
                                    title: '微信好友测试链接',
                                    description: '分享自:江清清的技术专栏(www.lcode.org)',
                                    thumbImageUrl: 'http://mta.zttit.com:8080/images/ZTT_1404756641470_image.jpg',
                                    webpageUrl: 'http://www.lcode.org',
                                    scene: 1,
                                }).catch((error) => {
                                    Modal.alert('提示', error.errStr);
                                });
                            } else {
                                Modal.alert('提示', '没有安装微信软件，请您安装微信之后再试');
                            }
                        });
                    }}
                />
                <CustomButton
                    text="取值 "
                    onPress={() => {
                        this.GetAppParameters();
                    }}
                />
                <CustomButton
                    text="微信支付定金 调用"
                    onPress={() => {
                        //支付前的准备，如车位状态，当前订单状态
                        let paydata = {order_id: 249, payprice: 800};
                        ownerPaymentBefore(paydata).then((paybefore) => {
                            //支付前判断状态等
                            if (paybefore.code === 0) {
                                WeChat.isWXAppInstalled().then((isInstalled) => {
                                    //微信是否安装async
                                    if (isInstalled) {
                                        getAppParameters(paydata).then(async (par) => {
                                            //生成统一下单，成功后打开微信支付
                                            if (par.code === 0) {
                                                let params = JSON.parse(par.data);
                                                try {
                                                    await WeChat.pay(params)
                                                        .then((pay) => {
                                                            //打开支付，判断是否支付成功await
                                                            if (pay.errCode === 0) {
                                                                ownerPayment(paydata).then((busi) => {
                                                                    //支付完成后，处理业务
                                                                    if (busi.code === 0) {
                                                                        console.log('收钱成功后，业务回调成功');
                                                                    } else {
                                                                        console.log('收钱成功后，但业务回调失败');
                                                                    }
                                                                });
                                                            } else {
                                                                console.log('支付失败，异常');
                                                            }
                                                        })
                                                        .catch((err) => {
                                                            Modal.alert('提示', '支付失败，原因：' + err);
                                                        });
                                                } catch (error) {
                                                    Modal.alert('提示', '失效，支付异常!');
                                                }
                                            } else {
                                                Modal.alert('提示', par.msg);
                                            }
                                        });
                                    } else {
                                        Modal.alert('提示', '没有安装微信软件，请您安装微信之后再试');
                                    }
                                });
                            } else {
                                Modal.alert('提示', paybefore.msg);
                            }
                        });
                    }}
                />
            </View>
        );
    }
}
const styles = StyleSheet.create({
    welcome: {
        fontSize: 20,
        textAlign: 'center',
        margin: 10,
    },
    button: {
        margin: 5,
        backgroundColor: 'white',
        padding: 15,
        borderBottomWidth: StyleSheet.hairlineWidth,
        borderBottomColor: '#cdcdcd',
    },
});

export default RNWeChatDemo;
//AppRegistry.registerComponent('RNWeChatDemo', () => RNWeChatDemo);
